package com.sg.web;

import com.sg.bean.Business;
import com.sg.bean.Commodity;
import com.sg.bean.CommodityCateGorp;
import com.sg.bean.Shop;
import com.sg.service.*;
import com.sg.util.NewFile;
import com.sg.vo.BusinessVO;
import com.sg.vo.CommodityVO;
import com.sg.vo.ShopVO;
import com.sg.vo.UserVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author wangcf
 * @Description
 * @date 2023/3/15 12:23
 */
@Controller
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private CommodityCateGorpService commodityCateGorpService;
    @Autowired
    private BusinessService businessService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopCommodityService shopCommodityService;
    Logger logger = Logger.getLogger(CommodityController.class);



    @RequestMapping("commoditylistbybusinessId")
    public String commoditylistbybusinessId(int businessId, Model model,HttpSession session) {
        logger.info("进入商品全部列表控制器");
        List<CommodityVO> commodityVOS = commodityService.findByBusinessId(businessId);//根据商家id查到所有商品
        //根据商家编号和用户id查询购物车
        Object obj = session.getAttribute("createUser");
        UserVO user = (UserVO) obj;
        if (user != null) {
            List<ShopVO> list = shopService.findByUidAndBusinessId(user.getuId(), businessId);//查到购物车所有商品
            model.addAttribute("shopcommoditylist", list);//显示给用户
        }
        model.addAttribute("businessId",businessId);
        model.addAttribute("commoditylist", commodityVOS);
        return "commoditylist";
    }

    @RequestMapping("commoditylistbycateid")
    public String commoditylistbycateid(int cateId, Model model,HttpSession session) {
        logger.info("进入根据分类编号查询商品的控制器");
        List<CommodityVO> commodityVOS = commodityService.findByCateId(cateId);
        CommodityCateGorp commodityCateGorp=commodityCateGorpService.findByCateId(cateId);
        //根据商家编号和用户id查询购物车
        Object obj = session.getAttribute("createUser");
        UserVO user = (UserVO) obj;
        if (user != null) {
            List<ShopVO> list = shopService.findByUidAndBusinessId(user.getuId(), commodityCateGorp.getBusinessId());//查到购物车所有商品
            model.addAttribute("shopcommoditylist", list);//显示给用户
        }
        model.addAttribute("businessId",commodityCateGorp.getBusinessId());
        model.addAttribute("commoditylist", commodityVOS);
        return "commoditylist";
    }

    @RequestMapping("thirdly/tocommoditycategorpadd")
    public String tocommoditycategorpadd() {
        logger.info("添加商品分类回显控制器");
        return "thirdly/commoditycategorpadd";
    }

    /**
     * 未完成
     *
     * @param commodityCateGorp
     * @param session
     * @return
     */
    @RequestMapping("thirdly/commoditycategorpadd")
    public String commoditycategorpadd(CommodityCateGorp commodityCateGorp, HttpSession session) {
        logger.info("进入到添加商品分了控制器");
        boolean i = commodityCateGorpService.add(commodityCateGorp);
        logger.info(i);
        Object obj = session.getAttribute("business");
        BusinessVO business = (BusinessVO) obj;
        return "redirect:../businessindex.action?businessId=" + business.getBusinessId();
    }

    @RequestMapping("thirdly/tocommodityadd")
    public String tocommodityadd(HttpSession session, Model model) {
        Object obj = session.getAttribute("business");
        BusinessVO business = (BusinessVO) obj;
        logger.info(business);
        List<CommodityCateGorp> commodityCateGorps = commodityCateGorpService.findByBusinessId(business.getBusinessId());
        model.addAttribute("commoditycategorplist", commodityCateGorps);
        return "thirdly/commodityadd";
    }

    @RequestMapping("thirdly/commodityadd")
    public String commodityadd(Commodity commodity, @RequestParam("file") CommonsMultipartFile file, HttpSession session) throws IOException {
        logger.info("进入到增加菜品控制器");
        /*Commodity commodity =new Commodity();
        commodity.setComName(comName);
        commodity.setComPrice(comPrice);
        commodity.setComStateId(comStateId);
        commodity.setCateId(cateId);*/
        logger.info(commodity);
        logger.info(file);
        logger.info(file.getSize());
        Object obj = session.getAttribute("business");
        BusinessVO business = (BusinessVO) obj;
        if (file != null && file.getSize() > 0) {
            File newFile = NewFile.getFile(file);
            logger.info(newFile);
            file.transferTo(newFile);//上传到目标位置
            commodity.setComImg(NewFile.getNewFileNameAnd(newFile.getName()));//设置商品图片
            logger.info(commodity.getComImg());
        }
        boolean i = commodityService.add(commodity);
        logger.info(i);
        return "redirect:../businessindex.action?businessId=" + business.getBusinessId();
    }

    @RequestMapping("thirdly/commoditydel")
    public String commoditydel(int comId, HttpSession session) {
        logger.info("进入到下架商品控制器");
        Object obj = session.getAttribute("business");
        BusinessVO business = (BusinessVO) obj;
        int[] arr = {comId};
        Commodity commodity = new Commodity();
        commodity.setComId(comId);
        commodity.setComStateId(3);
        boolean i = commodityService.update(commodity);
        logger.info(i);
        return "redirect:../commoditylistbybusinessId.action?businessId=" + business.getBusinessId();
    }


}






















